package com.open.api.peekapi.controller;

import com.open.api.peekapi.bean.ApiItem;
import com.open.api.peekapi.bean.ApiItemData;
import com.open.api.peekapi.bean.Tree;
import com.open.api.peekapi.services.ApiItemDataServices;
import com.open.api.peekapi.services.ApiItemServices;
import com.open.api.peekapi.util.LocalUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by mengdexuan on 2017/6/22 11:13.
 */
@Controller
@RequestMapping("/tree")
public class DataController {

	private static Logger log = LoggerFactory.getLogger(DataController.class);

	@Autowired
	ApiItemServices apiItemServices;
	@Autowired
	ApiItemDataServices apiItemDataServices;

	@RequestMapping("/data")
	public String index(HttpServletRequest request, Model model){
		return "data";
	}



	@RequestMapping("/async")
	@ResponseBody
	public List<Tree> async(HttpServletRequest request, Tree tree){

		String id = tree.getId();
		Integer level = tree.getLevel();
		log.info("节点 id : {}，节点层级：{}。",new Object[]{id,level});

		List<Tree> result = null;

		if (level==null)level=-1;

		if (level>0){
			List<ApiItemData> list = apiItemDataServices.data(id);
			result = LocalUtil.apiItemDataList2TreeList(list);
		}else {
			if (id==null){
				List<ApiItem> list = apiItemServices.firstLevelItem();
				result = LocalUtil.apiItemList2TreeList(list);
			}else {
				List<ApiItem> list = apiItemServices.secondLevelByFirstLevelId(id);
				result = LocalUtil.apiItemList2TreeList(list);
			}
		}

		return result;
	}





	@RequestMapping("/add")
	@ResponseBody
	public String add(HttpServletRequest request, ApiItemData data){

		apiItemDataServices.add(data);

		return "ok";
	}




	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){

		apiItemDataServices.del(id);

		return "ok";
	}







}






































